package com.fzy.core.codetools;

import org.apache.commons.lang.StringUtils;

import com.fzy.core.system.entity.Property;

public class PropertyWrapper {
	public static final String STRING = "string";
	public static final String LONG = "long";
	public static final String DATE = "date";
	public static final String NUMBER = "number";
//	public static final String DICT = "dict";
//	public static final String REF = "ref";
	
	public static final int LENGTH_DEFAULT = 255;
	public static final int LENGTH_MIN = 0;
	public static final int LENGTH_MAX = 4096;
	public static final int PRECISION_DEFAULT = 38;
	public static final int PRECISION_MIN = 0;
	public static final int PRECISION_MAX = 38;
	public static final int SCALE_DEFAULT = 4;
	public static final int SCALE_MIN = 0;
	public static final int SCALE_MAX = 38;
	Property data;
	EntityWrapper parent;
	
	public boolean getIsSavedField(){
		return Utils.isSavedProperty(getNameUncapital());
	}
	
	public boolean getHasSequence(){
		return !StringUtils.isBlank(getSequence());
	}
	
	public String getSequence(){
		String result = "";
		if(getIsKey()){
			result = data.getSeqName();
			if(!StringUtils.isBlank(result)){
				result = result.trim().toLowerCase();
			}
		}
		return result;
	}
	
	public boolean getHasDefaultValue(){
		return !StringUtils.isBlank(getDefaultValue());
	}
	
	public String getDefaultValue(){
		return StringUtils.defaultString(data.getDefaultValue());
	}
	
	public String getTitle(){
		String result = data.getTitle();
		if(StringUtils.isBlank(result)){
			if("status".equalsIgnoreCase(getNameUncapital())){
				result = "记录状态";
			}
			if("createdUser".equalsIgnoreCase(getNameUncapital())){
				result = "记录创建人";
			}
			if("createdDate".equalsIgnoreCase(getNameUncapital())){
				result = "记录创建时间";
			}
			if("updatedUser".equalsIgnoreCase(getNameUncapital())){
				result = "记录更新人";
			}
			if("updatedDate".equalsIgnoreCase(getNameUncapital())){
				result = "记录更新时间";
			}
		}
		if(StringUtils.isBlank(result)) result = data.getName();
		return result.trim().toLowerCase();
	}
	
	public boolean getNotNull(){
		return getIsKey() ? true : Utils.string2boolean(data.getIsNotNull(), false);
	}
	
	public boolean getIsKey(){
		return Utils.string2boolean(data.getIsKey(), false);
	}
	
	public boolean getIsDict(){
		return !StringUtils.isBlank(getDict());
	}
	
	public boolean getIsUnique(){
		return Utils.string2boolean(data.getIsUnique(), false);
	}
	
	public boolean getIsShow(){
		return Utils.string2boolean(data.getIsShow(), true);
	}
	
	public boolean getIsQuery(){
		return Utils.string2boolean(data.getIsQuery(), false);
	}
	
	public String getDict(){
		return StringUtils.defaultString(data.getDict()).trim();
	}
	
	public int getPrecision(){
		int result = Utils.string2int(data.getPrecision(), PRECISION_DEFAULT);
		if(result < PRECISION_MIN) result = PRECISION_MIN;
		if(result > PRECISION_MAX) result = PRECISION_MAX;
		return result;
	}
	
	public String getComment(){
		String result = StringUtils.isBlank(data.getComment()) ? getTitle() : data.getComment();
		result = result.trim();
		if(getIsKey() && getHasSequence()){
			result += "(sequence:"+getSequence()+")";
		}
		if(getIsDict()){
			result += "(dict:"+getDict()+")";
			if("yn".equalsIgnoreCase(data.getDict())){
				result += "(0,否,1,是)";
			}
		}
		if(getHasDefaultValue()){
			result += "(default:"+getDefaultValue()+")";
		}
		return result;
	}
	
	public int getLength(){
		int result = Utils.string2int(data.getLength(), LENGTH_DEFAULT);
		if(result < LENGTH_MIN) result = LENGTH_MIN;
		if(result > LENGTH_MAX) result = LENGTH_MAX;
		return result;
	}
	
	public int getScale(){
		int result = Utils.string2int(data.getScale(), SCALE_DEFAULT);
		if(result < SCALE_MIN) result = SCALE_MIN;
		if(result > SCALE_MAX) result = SCALE_MAX;
		return result;
	}
	
	public String getHbmType(){
		String result = "";
		if(STRING.equalsIgnoreCase(getPropertyType()))
			result = "type=\"string\" length=\""+getLength()+"\"";
		else if(LONG.equalsIgnoreCase(getPropertyType()))
			result = "type=\"long\"";
		else if(DATE.equalsIgnoreCase(getPropertyType()))
			result = "type=\"timestamp\"";
		else if(NUMBER.equalsIgnoreCase(getPropertyType()))
			result = "type=\"double\" precision=\""+getPrecision()+"\" scale=\""+getScale()+"\"";
//		else if(DICT.equalsIgnoreCase(getPropertyType()))
//			result = "type=\"string\" length=\""+LENGTH_DEFAULT+"\"";
//		else if(REF.equalsIgnoreCase(getPropertyType()))
//			result = "type=\"string\" length=\""+LENGTH_DEFAULT+"\"";
		
		return result;
	}
	
	public String getDbType(){
		String result = "";
		if(STRING.equalsIgnoreCase(getPropertyType()))
			result = "varchar("+getLength()+")";
		else if(LONG.equalsIgnoreCase(getPropertyType()))
			result = "bigint";
		else if(DATE.equalsIgnoreCase(getPropertyType()))
			result = "timestamp";
		else if(NUMBER.equalsIgnoreCase(getPropertyType()))
			result = "number("+getPrecision()+", "+getScale()+")";
//		else if(DICT.equalsIgnoreCase(getPropertyType()))
//			result = "varchar("+LENGTH_DEFAULT+")";
//		else if(REF.equalsIgnoreCase(getPropertyType()))
//			result = "varchar("+LENGTH_DEFAULT+")";

		return result;
	}
	
	public String getDbOracleType(){
		String result = "";
		if(STRING.equalsIgnoreCase(getPropertyType()))
			result = "varchar2("+getLength()+")";
		else if(LONG.equalsIgnoreCase(getPropertyType()))
			result = "integer";
		else if(DATE.equalsIgnoreCase(getPropertyType()))
			result = "date";
		else if(NUMBER.equalsIgnoreCase(getPropertyType()))
			result = "number("+getPrecision()+", "+getScale()+")";
//		else if(DICT.equalsIgnoreCase(getPropertyType()))
//			result = "varchar2("+LENGTH_DEFAULT+")";
//		else if(REF.equalsIgnoreCase(getPropertyType()))
//			result = "varchar2("+LENGTH_DEFAULT+")";

		return result;
	}
	
	public String getPropertyType(){
		return StringUtils.isBlank(data.getPropertyType()) ? STRING : data.getPropertyType().trim().toLowerCase();
	}
	
	public String getJavaType() {
		String result = getPropertyType();
		if(NUMBER.equalsIgnoreCase(result))
			result = "Double";
//		if(DICT.equalsIgnoreCase(result))
//			result = STRING;
//		if(REF.equalsIgnoreCase(result))
//			result = STRING;

		return StringUtils.capitalize(result);
	}
	
	public String getColumnName() {
		String result;
		if(!StringUtils.isBlank(data.getColumnName())){
			result = data.getColumnName();
		}else{
			result = Utils.camel2underline(data.getName());
		}
		return result.toLowerCase();
	}
	
	public String getNameCapital() {
		return StringUtils.capitalize(StringUtils.trim(data.getName()));
	}
	
	public String getNameUncapital() {
		return StringUtils.uncapitalize(StringUtils.trim(data.getName()));
	}
	////////////////////////////////// generated /////////////////////////////////////////////
	public Property getData() {
		return data;
	}
	public void setData(Property data) {
		this.data = data;
	}
	public EntityWrapper getParent() {
		return parent;
	}
	public void setParent(EntityWrapper parent) {
		this.parent = parent;
	}
	
}
